package com.wushuu.ce.ui.activity;

import android.support.v4.app.FragmentTabHost;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TabHost;
import android.widget.TextView;

import com.wushuu.ce.R;
import com.wushuu.ce.application.SApplication;
import com.wushuu.ce.interfaces.IBaseView;
import com.wushuu.ce.ui.fragment.MainPageFragment;
import com.wushuu.ce.ui.fragment.MyInfoFragment;
import com.wushuu.ce.ui.fragment.RankResultFragment;
import com.wushuu.ce.ui.fragment.StartExerciseFragment;
import com.wushuu.ce.utils.ResourceUtils;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;


@ContentView(R.layout.activity_main)
public class MainActivity extends BaseActivity implements View.OnTouchListener ,IBaseView{


    @ViewInject(R.id.main_activity)
    private LinearLayout main_activity;
    private FragmentTabHost mTabHost;
    private LayoutInflater mInflater;

    private Class<?>[] mFragmentAry = {MainPageFragment.class, StartExerciseFragment.class,
            RankResultFragment.class, MyInfoFragment.class};

    private int mImgAry[] = {R.mipmap.main_page_normal, R.mipmap.start_exercise_normal,
            R.mipmap.rank_result_normal, R.mipmap.my_market_normal};

    private int mImgAry2[] = {R.mipmap.main_page_selected, R.mipmap.start_exercise_selected,
            R.mipmap.rank_result_selected, R.mipmap.my_market_selected};

    private String mTxtAry[] = ResourceUtils.getStringArray(R.array.tab_names);

    private int mCount = 0;

    /**
     * 初始化组件
     */
    @Override
    public void init() {
        SApplication.getInstance().addActivity(this);

        main_activity.setOnTouchListener(this);
        mInflater = LayoutInflater.from(this);

        // 实例化TabHost对象，得到TabHost
        mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);

        mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);

        if(mCount == 0){
            mCount = mFragmentAry.length;
        }
        for (int i = 0; i < mCount; i++) {
            TabHost.TabSpec tabSpec = mTabHost.newTabSpec(mTxtAry[i]).setIndicator(getTabItemView(i));
            mTabHost.addTab(tabSpec, mFragmentAry[i], null);
        }
        mTabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener() {

            @Override
            public void onTabChanged(String tabId) {
                setTabItemContent(tabId);
            }
        });
    }


    /***
     * 设置tabItem的内容
     *
     * @param tabId
     */
    private void setTabItemContent(String tabId) {
        for (int i = 0; i < mCount; i++) {
            View view = mTabHost.getTabWidget().getChildAt(i);
            ImageView imageView = (ImageView) view.findViewById(R.id.tab_imageview);
            TextView textView = (TextView) view.findViewById(R.id.tab_textview);
            view.setBackgroundColor(getResources().getColor(R.color.main_activity_footerbar_bkg_color));

            if (mTxtAry[i].equals(tabId)) {
                imageView.setBackgroundResource(mImgAry2[i]);
                textView.setTextColor(getResources().getColor(R.color.main_activity_footerbar_text_color_clicked));
            } else {
                imageView.setBackgroundResource(mImgAry[i]);
                textView.setTextColor(getResources().getColor(R.color.main_activity_footerbar_text_color_normal));
            }
        }
    }

    /**
     * 给Tab按钮设置图标和文字
     *
     * @param index
     * @return
     */
    private View getTabItemView(int index) {
        View view = mInflater.inflate(R.layout.tab_item_view, null);

        ImageView imageView = (ImageView) view.findViewById(R.id.tab_imageview);
        TextView textView = (TextView) view.findViewById(R.id.tab_textview);
        view.setBackgroundColor(getResources().getColor(R.color.main_activity_footerbar_bkg_color));

        textView.setText(mTxtAry[index]);
        if (index == 0) {
            imageView.setBackgroundResource(mImgAry2[index]);
            textView.setTextColor(getResources().getColor(R.color.main_activity_footerbar_text_color_clicked));
        } else {
            imageView.setBackgroundResource(mImgAry[index]);
            textView.setTextColor(getResources().getColor(R.color.main_activity_footerbar_text_color_normal));
        }

        return view;
    }


    @Override
    public boolean onTouch(View v, MotionEvent event) {
        InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
        return imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
    }

    @Override
    public Object getInfo(int code) {
        return null;
    }

    @Override
    public void setInfo(Object obj, int code) {

    }
}
